1748B - Diverse Substrings - CodeForces Solution


brute force implementation strings *1400

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n = int(input())
    arr = [int(x) for x in input()]
    c = 0
    for i in range(n):
        l = [0]*10
        dist = 0
        maxi = 0
        for j in range(i,min(i+101,n)):
            l[arr[j]]+=1
            if(l[arr[j]]==1):
                dist+=1
            else:
                maxi = max(maxi,l[arr[j]])
            if(maxi<=dist):
                c+=1
    print(c)

C++ Code:

#include <bits/stdc++.h>
/*
 * Basic Runtimes Table:
 * +-----+-----------------+
 * |  n  | Worst Algorithm |
 * +-----+-----------------+
 * | 11  | O(n!), O(n^6)   |
 * | 18  | O(2^n * n^2)    |
 * | 22  | O(2^n * n)      |
 * | 100 | O(n^4)          |
 * | 400 | O(n^3)          |
 * | 2K  | O(n^2 log n)    |
 * | 10K | O(n^2)          |
 * | 1M  | O(n log n)      |
 * +-----+-----------------+
 */
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;

#define INF 100000000
#define MOD 1000000007
#define endl '\n'
#define LIST(az, bz) for (int cz = 0; cz < (int)(bz); cz++) cout << az[cz] << (cz == (int)(bz)-1 ? '\n' : ' ');

int T, N;

int main()
{
	// ios_base::sync_with_stdio(false);
	// cin.tie(NULL);
	// cout.tie(NULL);
	
	cin >> T;
	while (T--) {
		cin >> N;

		string s;
		cin >> s;

		int ans = 0;
		for (int n = 0; n < N; n++) {
			vi used(10);
			vi counts(10);

			int unique = 0, max_count = 1;
			for (int i = 0; i < 100 && n + i < N; i++) {
				int d = s[n+i] - '0';
				if (!used[d]) {
					used[d] = true;
					unique++;	
				}
				counts[d]++;
				max_count = max(max_count, counts[d]);
				if (unique >= max_count) {
					ans++;
				}
			}
		}
		
		cout << ans << endl;
	}
}


Comments

Submit
0 Comments
More Questions

1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String
1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach